
Ufida.SubjectCombox = function(conf) {
	Ufida.SubjectCombox.instance = this;
	function isInitedFinance(){return 0;};
	var self = this;
	self.subTypeErrText = '只能选择资产类科目.';//'科目类别必须与账户类型一致,请选择【{0}_{1}】的下级科目';
	self.subUsedText = '该科目已与现金银行账户【{0}】关联,请新增或选择其它科目';
	

	// 科目选择combox
	var subItem = Ext.data.Record.create([ {
		name : 'id'
	},{	name : 'no'
	},{	name : 'text'
	},{	name : 'pre'
	},{	name : 'leaf'
	},{	name : 'usedInBankAccount'
	}]);
	this.subItem=subItem;
	var subds = new Ext.data.Store( {
		proxy : new Ext.data.HttpProxy( {
			url : '/accountMgr/subject.do?method=query'
		}),
		reader : new Ext.data.JsonReader( {
			totalProperty : 'result.totalCount',
			root : 'result.result'
		}, subItem)
	});
	this.subds=subds;
	/*var resultTpl = ITEM_IDS.contains('') ?// 没有财务处理权限时不显示'新增或选择科目'链接	--add by tongliang 20090216
			new Ext.XTemplate(
			'<div class="add-item"><a href="javascript:void(0);" onclick="javascript:Ufida.SubjectCombox.instance.editSub();">新增或选择科目</a></div><tpl for=".">',
			'	<div class="search-item" onmouseover="Ufida.SubjectCombox.instance.showTip(\'{leaf}\',\'{usedInBankAccount}\',this)">',
			'		<span class="subject-noUsed{usedInBankAccount} subject-leaf-{leaf}">{pre}{no}_{text}</span>',
			'	</div>' + '</tpl>')
			: 
			new Ext.XTemplate(
			'<tpl for=".">',
			'	<div class="search-item" onmouseover="Ufida.SubjectCombox.instance.showTip(\'{leaf}\',\'{usedInBankAccount}\',this)">',
			'		<span class="subject-noUsed{usedInBankAccount} subject-leaf-{leaf}">{pre}{no}_{text}</span>',
			'	</div>' + '</tpl>');
	*/
	/*目前暂时不添加帐务处理中新增 --add by madi 20090510*/
	var editSubjectLinkId = Ext.id();	
	var bankAccTpl = new Ext.XTemplate(
	'<div class="add-item"><a href="javascript:void(0);" id="'+editSubjectLinkId+'">新增或选择科目</a></div><tpl for=".">',
	'	<div class="search-item" onmouseover="Ufida.SubjectCombox.instance.showTip(\'{leaf}\',\'{usedInBankAccount}\',this)">',
	'		<span class="subject-noUsed{usedInBankAccount} subject-leaf-{leaf}">{pre}{no}_{text}</span>',
	'	</div>' + '</tpl>');	
	var client_providerTpl = new Ext.XTemplate(
	'<div class="add-item"><a href="javascript:void(0);" id="'+editSubjectLinkId+'">新增或选择科目</a></div><tpl for=".">',
	'	<div class="search-item" onmouseover="Ufida.SubjectCombox.instance.showTip(\'{leaf}\',\'{usedInBankAccount}\',this)">',
	'		<span subject-leaf-{leaf}">{pre}{no}_{text}</span>',
	'	</div>' + '</tpl>');
	var resultTpl = null ;
	if( this.baseType == 'bankAccount'){
		resultTpl = bankAccTpl ;
	}else{
		resultTpl = client_providerTpl ;
	}
	
	Ufida.SubjectCombox.superclass.constructor.call(this, {
		fieldLabel : isInitedFinance() ? '科目编号:' : '',
		allowBlank : isInitedFinance() ? this.allowBlank : true,
		disabled : isInitedFinance() ? false : true,
		hidden : isInitedFinance() ? false : true,
		labelSeparator : '',
		readOnly : true,
		msgTarget : 'under',
		width : 180,
		store : subds,
		displayField : 'no',
		typeAhead : false,
		triggerAction : 'all',
		loadingText : '查找中...',
		model : 'local',
		hideTrigger : false,
		tpl : resultTpl,
		itemSelector : 'div.search-item',
		cls : 'text-align:left',
		queryDelay : 0,
		selectOnFocus : false,
		enableKeyEvents : true,
		minChars : 1,
		valueNotFoundText : '没有相关科目',
		listeners : {
			'select' : function(combo, record, index) {
				var id = record.get('id');
				var no = record.get('no');
				no = no || '';
				self.fireEvent("selectSubject", id,no);
			},
			'show' : function() {
				setTimeout(function() {
					self.focus();
				}, 10);
			},
			'collapse' : function() {
				if (Ufida.BalloonBox)
					Ufida.BalloonBox.hide();
			}
		}
	});
	this.addEvents( {
		'selectSubject' : true
	});

	// 选择科目时弹出的选择科目面板
	this.newWin ;
	var clickBindFlag = false ;
	this.on('expand',function(){
		if(this.list && !clickBindFlag){
			this.list.on('click',function(e,t){
				try{
					if (t.id && t.id == editSubjectLinkId ) {
						this.editSub();
					}
				}catch(e){}
			},this);
			clickBindFlag = true ;
		}
	},this);
	Ext.apply(this, conf);
}
Ext.extend(Ufida.SubjectCombox, Ext.form.UComboBox, {
	onViewClick : function(doFocus) {
		var self=this;
		var index = this.view.getSelectedIndexes()[0];
		var r = this.store.getAt(index);

		if (r) {
			var leaf = r.get('leaf');
			if (leaf == 'Y') {
				var no = r.get('no');
				var usedInBankAccount = r.get('usedInBankAccount');
				if ( this.baseType =='bankAccount' && usedInBankAccount) {
					if (Ufida.BalloonBox){
						return Ufida.BalloonBox.show(self.subUsedText.format(usedInBankAccount), '',self.el, 'bl-tl');
					}
				}
				this.onSelect(r, index);
				if (Ufida.BalloonBox)
					Ufida.BalloonBox.hide();
			} else {
				if (Ufida.BalloonBox)
					return Ufida.BalloonBox.show('不能选择非末级科目!', '', self.el,'bl-tl');
			}
		} else if (r == undefined) {
			this.collapse();
		}
		if (doFocus !== false) {
			this.el.focus();
		}
	},
	doQuery : function(q, forceAll) {
		if (q === undefined || q === null) {
			q = '';
		}
		var qe = {
			query : q,
			forceAll : forceAll,
			combo : this,
			cancel : false
		};
		if (this.fireEvent('beforequery', qe) === false || qe.cancel) {
			return false;
		}
		q = qe.query;
		forceAll = qe.forceAll;
		if (forceAll === true || (q.length >= this.minChars)) {
			this.lastQuery = q;
			if (this.mode == 'local') {
				this.selectedIndex = -1;
				if (forceAll) {
					this.store.clearFilter();
				} else {
					this.store.filter(this.displayField, q);
				}
				this.onLoad();
			} else {
				this.store.baseParams[this.queryParam] = q;
				this.store.load( {
					params : this.getParams(q)
				});
				this.expand();
			}
		}
	},
	showTip : function(leaf, usedInBankAccount, dom) {
		if( this.hideTip === true ){
			dom.removeAttribute('title');
			return ;
		}
		if (leaf == 'Y') {
			if (usedInBankAccount) {
				dom.setAttribute('title', "已关联现金银行账户【{0}】".format(usedInBankAccount));
			} else {
				dom.setAttribute('title', "未关联");
			}
		} else {
			dom.setAttribute('title', '非末级科目');
		}
	},
	editSub:function() {
		var self=this;
		self.subWin();
		self.collapse();// 收起下拉列表
	},
	subWin : function() {
		var self = this;
		var vGrid = self;
		var choseSub = self.choseSub;
		
		if(!this.panel)
		   	this.panel = new Ufida.SubjectPanel();
		var panel = self.panel;   	
		var subds=self.subds;
		// 增加时弹出窗口
		if (!self.newWin) {
			panel.subTab.setSize(300, 490)
			panel.form.setSize(400, 490)
			for (var i = 0;i < panel.trees.length; i++) {
				var tree = panel.trees[i];
				tree.on('dblclick', function() {
					vGrid.choseSub(this);
				}, tree)
			}
			self.newWin = new Ext.Window({
				width : 740,
				height : 580,
				layout : 'border',
				closeAction : 'hide',
				title : '科目',
				maskDisabled : false,
				items : [panel.subTab, panel.form],
				buttonAlign : 'center',
				buttons : [
						{
							text : '确定',
							disabled : false,
							handler : function() {
								var selected = panel.subTab.getActiveTab().getSelectionModel().getSelectedNode();
								if (selected) {
									if (selected.leaf) {
										vGrid.choseSub(panel.subTab.getActiveTab());
									} else {
										return displayMessageBox('信息','不能选择非末级科目!');
									}
								}
							},
							scope : this
						}, {
							text : '关闭',
							handler : function() {
								self.newWin.hide();
							}
						}],
				listeners : {
					'hide' : function() {
						subds.reload();
					}
				}
			});
		}
		self.newWin.show();
	},
	choseSub:function(tree){
		var self = this;
		var newWin=self.newWin;
		
		var selected = tree.getSelectionModel().getSelectedNode();
		if (selected == null || !selected.leaf)
			return ;
		var subId=selected.id;
		var subNo = selected.text.split(subline)[0];
		var subText = selected.text.split(subline)[1];
		newWin.hide();
		self.fireEvent('selectSubject',subId,subNo);
	}
})

